package org.test.client.ui.component.combox;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;

import org.test.client.listener.CommonMouseListener;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

public class ItemLabel extends JLabel{
	private ItemLabel thisLabel = this;
	
	public static Font txtFont = new Font(Font.DIALOG, Font.PLAIN, 12);
	public static Font titleFont = new Font(Font.DIALOG, Font.BOLD, 13);
	public static Color c1 = Icons.getColor("3875d7");
	public static Color c2 = Icons.getColor("2a62bc");
	public static Color f1 = Color.black;
	public static Color f2 = Color.white;
	

	public String txt;
	public boolean title;
	public int defaultW = 0;
	public int defaultH = 0;
	public MouseListener listener = null;
	public boolean rollOvered = false;
	
	public MyCombox combox = null;
	public Object representObj = null;
	
	
	public ItemLabel(String txt, boolean title) {
		super(txt);
		this.txt = txt;
		this.title = title;
		if(title){
			this.setFont(titleFont);
			this.setHorizontalAlignment(this.LEFT);
		}else{
			this.setFont(txtFont);
			this.setHorizontalAlignment(this.CENTER);
			listener = new MouseAdapter() {
				@Override
				public void mouseEntered(MouseEvent mouseevent) {
					rollOvered = true;
					thisLabel.repaint();
				}
				
				@Override
				public void mouseExited(MouseEvent mouseevent) {
					rollOvered = false;
					thisLabel.repaint();
				}
				
				@Override
				public void mouseClicked(MouseEvent mouseevent) {
					if(combox != null ){
						combox.setSelectedObj(representObj);
					}else{
						System.out.println(representObj);
					}
				}
			};
			this.addMouseListener(listener);
//			this.setBorder(new LineBorder(Color.black, 1));
		}
		defaultW = UIUtil.getStringWidth(txt, this.getFontMetrics(this.getFont()));
		defaultH = UIUtil.getTxtHeight(this.getFontMetrics(this.getFont()))+4;
		this.setPreferredSize(new Dimension(0,defaultH));
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		int w = this.getWidth();
		int h = this.getHeight();
		
		if(rollOvered){
			Paint paint = UIUtil.getPaint(c1, c2, 0, 0,w, h);
			g2d.setPaint(paint);
			g2d.fillRect(0, 0, w, h);
			this.setForeground(f2);
		}else{
			setForeground(f1);
		}
		
		super.paintComponent(g);
	}
	
}
